__author__ = 'Janos G. Hajagos'

import ObjGraphicFile as ogf
from Materials import *
from GeometricForms import *
import networkx as nx
import DrawTextImage

def create_labelled_node(g,node, color=(0.8,0.8,0.8),width=256,height=256):
    if "Label" in g.node[node]:
        text = g.node[node]["Label"]
        img_obj = DrawTextImage.DrawTextImage(width,height,background_color=color,font_size=35)
        img_obj.write_text(text)
        file_name = str(node) + ".png"
        img_obj.write_file(file_name)

        diffuse_image_map = Material(node,Diffuse(image_map=ImageMap(file_name)))
        return diffuse_image_map

class UnDirectedGraphDraw(object):
    positions = None
    dimensions = 3

    def generate_obj_file(self, obj_filename, cube_dimension = 0.02,edge_height = 0.005, node_material = None, edge_material = None,node_material_func=None,edge_material_func = None):
        """Generates an OBJ file"""
        object_list = []
        i = 0

        list_of_material = []
        if node_material:
            list_of_material.append(node_material)
        else:
            list_of_material.append(Material("nodem", Diffuse(Green())))

        if edge_material:
            list_of_material.append(edge_material)
        else:
            list_of_material.append(Material("edgem", Diffuse(Blue())))

        for node in self.graph.nodes():
            if node in self.positions:
                vector = self.positions[node]
                x,y,z = vector[0], vector[1], vector[2]
                xc = x - cube_dimension / 2.0
                yc = y - cube_dimension / 2.0
                zc = z - cube_dimension / 2.0

                if node_material:
                    this_node_material = NamedMaterial(node_material.name())
                else:
                    if node_material_func:
                        this_node_material = node_material_func(self.graph,node)
                    else:
                        this_node_material = NamedMaterial("nodem")

                node_obj = Cube(xc, yc, zc, cube_dimension, this_node_material)
                object_list.append(node_obj)
                i += 1
        j = 0
        for edge in self.graph.edges():
            e1 = edge[0]
            e2 = edge[1]
            e1pos = self.positions[e1]
            e2pos = self.positions[e2]
            mp12 = e1pos + 0.5 * (e2pos - e1pos)

            if edge_material:
                this_edge_material = NamedMaterial(edge_material.name())
            else:
                this_edge_material  = NamedMaterial("edgem")

            quad = Quadrilateral(e1pos, mp12 + edge_height, e2pos, mp12 - edge_height, this_edge_material)
            object_list.append(quad)
            j += 1

        file_name_base = obj_filename
        mtl_file_name = file_name_base + ".mtl"
        f = open(file_name_base,"w")
        fm = open(mtl_file_name,"w")

        obj = ogf.ObjGraphicFileWriter(object_list,list_of_materials=list_of_material)
        obj.generate_file(f,file_name_base + ".mtl", fm)

class SpringLayout(UnDirectedGraphDraw):
    def __init__(self, graph):
        self.graph = graph

    def three_dimensional_cube_position(self,iterations = 50):
        self.positions = nx.spring_layout(self.graph, 3, iterations=iterations)
        self.dimensions = 3